<template>
  <div class="hire-page">
    <div class="page-header">
      <h1>当前招聘岗位</h1>
      <div class="action-buttons">
        <el-button type="success" @click="handleAddJob">
          <el-icon><Plus /></el-icon> 新增岗位
        </el-button>
      </div>
    </div>

    <div class="department-groups">
      <div
          class="department-card"
          v-for="(jobs, department) in groupedJobs"
          :key="department"
      >
        <div class="department-header">
          <h3>{{ department }}</h3>
          <span class="job-count">{{ jobs.length }} 个岗位</span>
        </div>

        <!-- 岗位列表 -->
        <div class="job-list-container">
          <div class="job-list">
            <div
                class="job-item"
                v-for="job in jobs"
                :key="job.id"
                @click="showJobDetail(job)"
            >
              <div class="job-info">
                <h4 class="job-title">{{ job.position }}</h4>
                <div class="job-meta">
                  <span class="salary" style="color: red;">{{ job.salary }}</span>
                  <span class="education">{{ job.education }}</span>
                  <span class="experience">{{ job.experience }}</span>
                </div>
              </div>
              <el-button
                  type="text"
                  class="delete-btn"
                  @click.stop="handleDeleteJob(job.id)"
              >
                <el-icon><Delete /></el-icon>
              </el-button>
            </div>
          </div>
        </div>
      </div>
    </div>

    <!-- 岗位详情对话框 -->
    <el-dialog
        v-model="detailVisible"
        :title="currentJob ? `${currentJob.department} - ${currentJob.position}` : '岗位详情'"
        :width="700"
        :close-on-click-modal="false"
        :lock-scroll="false"
        style="font-weight: bold"
    >
      <div class="job-detail">
        <div class="basic-info">
          <div class="info-item">
            <span class="label">薪资：</span>
            <template v-if="isEditing">
              <el-input v-model="currentJob.salary" class="edit-input" />
            </template>
            <template v-else>{{ currentJob.salary }}</template>
          </div>
          <div class="info-item">
            <span class="label">学历要求：</span>
            <template v-if="isEditing">
              <el-select v-model="currentJob.education" class="edit-select">
                <el-option label="大专" value="大专" />
                <el-option label="本科" value="本科" />
                <el-option label="硕士" value="硕士" />
                <el-option label="博士" value="博士" />
              </el-select>
            </template>
            <template v-else>{{ currentJob.education }}</template>
          </div>
          <div class="info-item">
            <span class="label">经验要求：</span>
            <template v-if="isEditing">
              <el-input v-model="currentJob.experience" class="edit-input" />
            </template>
            <template v-else>{{ currentJob.experience }}</template>
          </div>
        </div>

        <!-- 技能要求 -->
        <div class="detail-section">
          <h5>技能掌握</h5>
          <template v-if="isEditing">
            <el-input
                v-model="currentJob.skills"
                placeholder="请输入技能，用逗号分隔"
                class="edit-input full-width"
            />
          </template>
          <template v-else>
            <div class="skills-container">
              <el-tag
                  v-for="skill in currentJob.skills.split(',')"
                  :key="skill"
                  type="success"
              >
                {{ skill.trim() }}
              </el-tag>
            </div>
          </template>
        </div>

        <!-- 具体要求 -->
        <div class="detail-section">
          <h5>具体要求</h5>
          <template v-if="isEditing">
            <el-input
                v-model="currentJob.requirements"
                type="textarea"
                :rows="6"
                class="edit-textarea"
            />
          </template>
          <template v-else>
            <div class="requirements-content">
              {{ currentJob.requirements }}
            </div>
          </template>
        </div>
      </div>

      <template #footer>
        <div class="dialog-footer">
          <el-button @click="detailVisible = false">关闭</el-button>
          <template v-if="isEditing">
            <el-button @click="cancelEdit">取消</el-button>
            <el-button type="primary" @click="saveJobEdit">保存</el-button>
          </template>
          <template v-else>
            <el-button type="primary" @click="enterEditMode">编辑</el-button>
          </template>
        </div>
      </template>
    </el-dialog>

    <el-dialog
        v-model="editVisible"
        :title="isAddMode ? '新增岗位' : '编辑岗位'"
        :width="600"
        :close-on-click-modal="false"
        :lock-scroll="false"
    >
      <el-form :model="formJob" label-width="100px" class="job-form">
        <el-form-item label="所属部门" prop="department">
          <el-select v-model="formJob.department" class="form-select">
            <el-option label="研发部" value="研发部" />
            <el-option label="测试部" value="测试部" />
            <el-option label="市场部" value="市场部" />
            <el-option label="业务部" value="业务部" />
            <el-option label="人事部" value="人事部" />
          </el-select>
        </el-form-item>
        <el-form-item label="岗位名称" prop="position">
          <el-input v-model="formJob.position" />
        </el-form-item>
        <el-form-item label="薪资范围" prop="salary">
          <el-input v-model="formJob.salary" placeholder="例如：15k-25k" />
        </el-form-item>
        <el-form-item label="学历要求" prop="education">
          <el-select v-model="formJob.education">
            <el-option label="大专" value="大专" />
            <el-option label="本科" value="本科" />
            <el-option label="硕士" value="硕士" />
            <el-option label="博士" value="博士" />
          </el-select>
        </el-form-item>
        <el-form-item label="经验要求" prop="experience">
          <el-input v-model="formJob.experience" placeholder="例如：3-5年" />
        </el-form-item>
        <el-form-item label="技能掌握" prop="skills">
          <el-input v-model="formJob.skills" placeholder="用逗号分隔多个技能" />
        </el-form-item>
        <el-form-item label="具体要求" prop="requirements">
          <el-input
              v-model="formJob.requirements"
              type="textarea"
              :rows="4"
          />
        </el-form-item>
      </el-form>
      <template #footer>
        <el-button @click="editVisible = false">取消</el-button>
        <el-button type="primary" @click="confirmJobForm">确认</el-button>
      </template>
    </el-dialog>
  </div>
</template>

<script setup>
import { ref, reactive, computed, toRaw } from 'vue';
import { ElMessage, ElMessageBox } from 'element-plus';
import {
 Plus, Delete
} from '@element-plus/icons-vue';

// 模拟岗位数据
const jobs = reactive([
  {
    id: 1,
    department: '研发部',
    position: '前端开发工程师',
    salary: '15k-25k',
    education: '本科',
    experience: '3-5年',
    skills: 'Vue, JavaScript, HTML, CSS',
    requirements: '1. 负责公司前端业务开发与优化\n2. 参与前端架构设计与技术选型\n3. 优化用户体验与页面性能',
  },
  {
    id: 2,
    department: '研发部',
    position: '后端开发工程师',
    salary: '18k-30k',
    education: '本科',
    experience: '3-5年',
    skills: 'Java, Spring Boot, MySQL, Redis',
    requirements: '1. 负责后端接口开发与维护\n2. 参与数据库设计与优化\n3. 保证系统高可用性与稳定性',
  },
  {
    id: 3,
    department: '测试部',
    position: '软件测试工程师',
    salary: '12k-20k',
    education: '本科',
    experience: '1-3年',
    skills: 'Selenium, Jmeter, 测试用例设计',
    requirements: '1. 负责产品功能测试与性能测试\n2. 编写测试计划与测试报告\n3. 推动问题解决与质量改进',
  },
  {
    id: 4,
    department: '市场部',
    position: '市场专员',
    salary: '8k-15k',
    education: '本科',
    experience: '1-3年',
    skills: '数据分析, 活动策划, 沟通能力',
    requirements: '1. 负责市场活动策划与执行\n2. 分析市场数据与用户反馈\n3. 拓展合作渠道与资源',
  },
  {
    id: 5,
    department: '人事部',
    position: '招聘专员',
    salary: '7k-12k',
    education: '本科',
    experience: '1-3年',
    skills: '招聘, 面试, 人力资源管理',
    requirements: '1. 负责公司各岗位招聘工作\n2. 拓展与维护招聘渠道\n3. 优化招聘流程与候选人体验',
  }
]);

// 状态管理
const detailVisible = ref(false);
const editVisible = ref(false);
const isEditing = ref(false);
const isAddMode = ref(false);
const currentJob = ref(null);
const formJob = ref({});
const originalJob = ref(null);

// 按部门分组
const groupedJobs = computed(() => {
  const groups = {};
  jobs.forEach(job => {
    if (!groups[job.department]) {
      groups[job.department] = [];
    }
    groups[job.department].push(job);
  });
  return groups;
});

const showJobDetail = (job) => {
  currentJob.value = { ...job };
  originalJob.value = { ...job };
  isEditing.value = false;
  detailVisible.value = true;
};

// 进入编辑模式
const enterEditMode = () => {
  isEditing.value = true;
};

// 取消编辑
const cancelEdit = () => {
  currentJob.value = { ...originalJob.value };
  isEditing.value = false;
};

// 保存编辑
const saveJobEdit = () => {
  const index = jobs.findIndex(item => item.id === currentJob.value.id);
  if (index !== -1) {
    jobs[index] = { ...currentJob.value };
    ElMessage.success('岗位信息已更新');
    isEditing.value = false;
  }
};

const handleAddJob = () => {
  isAddMode.value = true;
  formJob.value = {
    department: '',
    position: '',
    salary: '',
    education: '',
    experience: '',
    grade: '',
    skills: '',
    requirements: ''
  };
  editVisible.value = true;
};

// 确认表单提交
const confirmJobForm = () => {
  if (isAddMode.value) {
    const newId = Math.max(...jobs.map(j => j.id), 0) + 1;
    jobs.push({
      id: newId,
      ...formJob.value
    });
    ElMessage.success('岗位已新增');
  } else {
    const index = jobs.findIndex(item => item.id === formJob.value.id);
    if (index !== -1) {
      jobs[index] = { ...formJob.value };
      ElMessage.success('岗位已更新');
    }
  }
  editVisible.value = false;
};

// 删除岗位
const handleDeleteJob = (id) => {
  const job = jobs.find(item => item.id === id);
  ElMessageBox.confirm(
      `确定要删除 ${job.department} - ${job.position} 吗？`,
      '删除确认',
      {
        confirmButtonText: '确认',
        cancelButtonText: '取消',
        type: 'warning'
      }
  ).then(() => {
    const index = jobs.findIndex(item => item.id === id);
    if (index !== -1) {
      jobs.splice(index, 1);
    }
    ElMessage.success('岗位已删除');
  }).catch(() => {
  });
};


</script>

<style scoped>
.hire-page {
  width: 100%;
  height: 100%;
  padding: 20px;
  box-sizing: border-box;
  background: #fff;
  border-radius: 10px;
  box-shadow: 0 2px 10px rgba(0, 0, 0, 0.05);

}

.page-header {
  height: 60px;
  display: flex;
  justify-content: space-between;
  align-items: center;
  margin-bottom: 20px;
}

.action-buttons {
  display: flex;
  gap: 10px;
}

.department-groups {
  display: flex;
  flex-direction: column;
  gap: 24px;
  height: calc(100% - 80px);
  overflow-y: auto; /* 整个部门列表的滚动条 */
  padding-right: 10px;
}

.department-card {
  background: #fff;
  border-radius: 10px;
  box-shadow: 0 2px 10px rgba(0, 0, 0, 0.05);
  overflow: hidden;
  transition: all 0.3s ease;
  display: flex;
  flex-direction: column;
}

.department-card:hover {
  box-shadow: 0 4px 16px rgba(0, 0, 0, 0.08);
}

.department-header {
  display: flex;
  align-items: center;
  padding: 16px 20px;
  background-color: #f0f7ff;
  border-bottom: 1px solid #e6f7ff;
  justify-content: space-between;
}

.department-header h3 {
  margin: 0;
  font-size: 16px;
  color: #1890ff;
  font-weight: 600;
}

.job-count {
  padding: 3px 10px;
  background-color: #e6f7ff;
  color: #1890ff;
  border-radius: 12px;
  font-size: 12px;
}

.job-list-container {
  max-height: 200px;
  overflow-y: auto;
  margin: 10px 0;
}

.job-list {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(300px, 1fr));
  gap: 15px;
  padding: 0 20px 10px;
}

.job-item {
  display: flex;
  justify-content: space-between;
  align-items: flex-start;
  padding: 15px;
  background-color: #fff;
  border: 1px solid #f0f0f0;
  border-radius: 8px;
  cursor: pointer;
  transition: all 0.2s ease;
  box-sizing: border-box;
}

.job-item:hover {
  border-color: #e6f7ff;
  background-color: #f9ffff;
  transform: translateY(-2px);
  box-shadow: 0 3px 8px rgba(0, 0, 0, 0.04);
}


.job-info {
  display: flex;
  flex-direction: column;
  gap: 8px;
}
.job-title {
  margin: 0;
  font-size: 16px;
  color: #333;
  font-weight: 500;
}
.job-meta {
  display: flex;
  gap: 10px;
  flex-wrap: wrap;
}


</style>